package cn.newtouch.system.service.impl;

import cn.newtouch.basic.service.impl.BaseServiceImpl;
import cn.newtouch.system.domain.Menu;
import cn.newtouch.system.domain.Role;
import cn.newtouch.system.mapper.MenuMapper;
import cn.newtouch.system.mapper.RoleMapper;
import cn.newtouch.system.service.IMenuService;
import cn.newtouch.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// @Transactional(readOnly = true,propagation = Propagation.SUPPORTS) 继承性
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role) {
       //保存三张表t_role ，t_role_menu  ，t_role_permission
        //先保存角色表 返回自增主键 其他两张表需要这个自增主键
        roleMapper.save(role);
        if(role.getPermissions().size()>0){
            //保存角色对应权限
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size()>0){
            //保存角色对应菜单
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }
    }

    @Override
    public void update(Role role) {
        //修改
        roleMapper.update(role);
        //先删除之前的权限和菜单
        roleMapper.removeRolePermission(role.getId());
        roleMapper.removeRoleMenu(role.getId());

        //后添加新的权限和菜单
        if(role.getPermissions().size()>0){
            //保存角色对应权限
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
        if(role.getMenus().size()>0){
            //保存角色对应菜单
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }

    }

    @Override
    public void remove(Long id) {
        //先删除外键关联的表 再删除自己
        roleMapper.removeEmployeeRole(id);
        roleMapper.removeRoleMenu(id);
        roleMapper.removeRolePermission(id);

        roleMapper.remove(id);
    }
}
